package com.qzsoft.system.modules.permission.rolebusiness.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.annotation.LogOperation;
import com.qzsoft.jeemis.common.constant.Constant;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.AssertUtils;
import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.jeemis.common.validator.group.AddGroup;
import com.qzsoft.jeemis.common.validator.group.DefaultGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;
import com.qzsoft.system.modules.permission.dept.dto.SysDeptDTO;
import com.qzsoft.system.modules.permission.rolebusiness.dto.SysRoleBusinessDTO;
import com.qzsoft.system.modules.permission.rolebusiness.dto.SysUserRoleBusinessDTO;
import com.qzsoft.system.modules.permission.rolebusiness.entity.SysRoleBusinessEntity;
import com.qzsoft.system.modules.permission.rolebusiness.service.SysRoleBusinessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 角色管理(SysRoleBusiness)表控制层
 *
 * @author sdmq
 * @since 2020-09-01 13:55:01
 */
@RestController
@RequestMapping("sys/role_business")
@Api(tags = "角色管理")
public class SysRoleBusinessController {
	@Autowired
	private SysRoleBusinessService sysRoleBusinessService;

	@GetMapping("tree")
	@ApiOperation("目录树")
//	@RequiresPermissions("sys:rolebusiness:list")
	public Result<List<SysRoleBusinessDTO>> tree(){
		List<SysRoleBusinessDTO> data = sysRoleBusinessService.treeFolder();
		return new Result<List<SysRoleBusinessDTO>>().ok(data);
	}

	@PostMapping("page")
	@ApiOperation("分页")
	@ApiImplicitParams({
			@ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
			@ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
			@ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "link_name", value = "名字", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "ge_createDate", value = "起始创建时间", paramType = "query", dataType = "Date"),
			@ApiImplicitParam(name = "le_createDate", value = "截止创建时间", paramType = "query", dataType = "Date")})
//	@RequiresPermissions("sys:rolebusiness:page")
	public Result<IPage<SysRoleBusinessEntity>> page(@ApiIgnore @RequestBody Map<String, Object> params) {
		IPage<SysRoleBusinessEntity> page = sysRoleBusinessService.page(params);

		return new Result<IPage<SysRoleBusinessEntity>>().ok(page);
	}

	@PostMapping("list")
	@ApiOperation("列表")
//	@RequiresPermissions("sys:rolebusiness:list")
	public Result<List<SysRoleBusinessEntity>> list(@ApiIgnore @RequestBody Map<String, Object> params) {
		List<SysRoleBusinessEntity> data = sysRoleBusinessService.list(params);

		return new Result<List<SysRoleBusinessEntity>>().ok(data);
	}

	@GetMapping("{id}")
	@ApiOperation("信息")
//	@RequiresPermissions("sys:rolebusiness:info")
	public Result<SysRoleBusinessEntity> get(@PathVariable("id") String id) {
		SysRoleBusinessEntity data = sysRoleBusinessService.get(id);

		return new Result<SysRoleBusinessEntity>().ok(data);
	}

	@PostMapping
	@ApiOperation("保存")
	@LogOperation("保存")
//	@RequiresPermissions("sys:rolebusiness:save")
	public Result save(@RequestBody SysRoleBusinessEntity dto) {
		//效验数据
		ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
		sysRoleBusinessService.save(dto);
		return new Result();
	}

	@PostMapping("save_group")
	@ApiOperation("保存角色分组")
	@LogOperation("保存角色分组")
//	@RequiresPermissions("sys:rolebusiness:save")
	public Result saveGroup(@RequestBody SysRoleBusinessEntity dto) {
		//效验数据
		ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
		String id = sysRoleBusinessService.saveGroup(dto);
		return new Result().ok(id);
	}

	@PutMapping
	@ApiOperation("修改")
	@LogOperation("修改")
//	@RequiresPermissions("sys:rolebusiness:update")
	public Result update(@RequestBody SysRoleBusinessEntity dto) {
		//效验数据
		ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

		sysRoleBusinessService.update(dto);

		return new Result();
	}

	@DeleteMapping
	@ApiOperation("删除")
	@LogOperation("删除")
//	@RequiresPermissions("sys:rolebusiness:delete")
	public Result delete(@RequestBody String[] ids) {
		//效验数据
		AssertUtils.isArrayEmpty(ids, "id");

		sysRoleBusinessService.delete(ids);

		return new Result();
	}


	@GetMapping("export")
	@ApiOperation("导出")
	@LogOperation("导出")
//	@RequiresPermissions("sys:rolebusiness:export")
	public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response)
			throws Exception {
		sysRoleBusinessService.exportXls(params, response);
	}
	/**
	 * 获取所有审批角色
	 *
	 * @return
	 */
	@GetMapping("get_sys_role_business")
	@ApiOperation("获取所有审批角色")
	@LogOperation("获取所有审批角色")
	public Result getSysRoleBusiness() {
		List<SysRoleBusinessDTO> sysRoleBusiness = sysRoleBusinessService.getSysRoleBusiness();
		return new Result<>().ok(sysRoleBusiness);
	}

	/**
	 * 根据部门父id获取子部门
	 *
	 * @return
	 */
	@GetMapping("get_sys_dept_business")
	@ApiOperation("根据部门父id获取子部门")
	@LogOperation("根据部门父id获取子部门")
	public Result getSysDeptBusiness(String pid) {
		List<SysDeptDTO> sysRoleBusiness = sysRoleBusinessService.getSysDeptBusiness(pid);
		return new Result<>().ok(sysRoleBusiness);
	}



	@GetMapping("get_user_by_businessanddept")
	@ApiOperation("审批角色和机构id查询审批人")
	@LogOperation("审批角色和机构id查询审批人")
	public Result getUserByRoleAndDept(String roleBusinessId, String deptId) {
		List<SysUserRoleBusinessDTO> userByRoleAndDept = sysRoleBusinessService.getUserByRoleAndDept(roleBusinessId, deptId);
		return new Result<>().ok(userByRoleAndDept);
	}

	/**
	 * 通过关键字进行模糊查询
	 *
	 * @param queryKey
	 * @return
	 */
	@GetMapping("get_user_by_querykey")
	@ApiOperation("通过关键字进行模糊查询")
	@LogOperation("通过关键字进行模糊查询")
	public Result getUserByQueryKey(String queryKey) {
		List<SysDeptDTO> userByRoleAndDept = sysRoleBusinessService.getUserByQueryKey(queryKey);
		return new Result<>().ok(userByRoleAndDept);
	}
	//====================流程平台通过审批角色和机构id查询审批人==end=====================================

}